﻿@page "/weather/{path?}"
@inject HttpClient Http
@inject IJSRuntime JS
@inject NavigationManager Navigation

<PageTitle>Weather</PageTitle>
<HeadContent>
    <style>
        .file {
            display: flex;
        }
        .file .file-list {
            flex: 1;
        }
        .file .file-op {
            width: 500px;
        }
    </style>
</HeadContent>

<h1>Weather</h1>

<p>This component demonstrates fetching data from the server.</p>

@if (rs == null || rs.Files == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <h3>@{
            if (rs.RootPath != null && rs.RootPath.Length > 0 && rs.RootPath.Length == rs.Paths.Length)
            {
                var paths = rs.Paths;
                var pathNames = rs.RootPath;
                foreach (var i in Enumerable.Range(0, rs.RootPath.Length))
                {
                    Console.WriteLine(i);

                    <NavLink href="@getUrl(paths[i])">@(pathNames[i])</NavLink> <span> / </span>
                }   
            }    
        }</h3>
    <div class="file">
    <div class="file-list">
            <table class="table">
                <thead>
                    <tr>
                        <th>名称</th>
                        <th>类型</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var forecast in rs.Files)
                    {
                        <tr>
                            <td>
                                @{
                                    if (forecast.IsDirectory)
                                    {
                                        <NavLink href="@getUrl(forecast.Path)">@forecast.Name</NavLink>
                                    }
                                    else
                                    {
                                        @forecast.Name
                                    }
                                }
                            </td>
                            <td>@(forecast.IsDirectory ? "目录" : "文件")</td>
                        </tr>
                    }
                </tbody>
            </table>
    </div>
    <div class="file-op"></div>

    </div>
}

@code {
    [Parameter]
    public string? path { get; set; } = "";



    private FileResponse? rs;

    protected override async Task OnInitializedAsync()
    {
        // await getRemoteData();
    }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
    }
    protected override async Task OnParametersSetAsync()
    {
        path = path ?? "";
        await getRemoteData();
    }
    private async Task getRemoteData()
    {
        rs = await Http.GetFromJsonAsync<FileResponse>("https://localhost:7161/api/Files/" + path);   
    }


    private async Task<string> encodeUrl(string name, string path)
    {
        var p = path + name;
        System.Console.WriteLine(p);
        var r = await JS.InvokeAsync<string>("encode64", p);
        return "weather/" + r;
    }
    private string getUrl(string path)
    {
        return "weather/" + path;
        
    }

    public class FileBaseInfo
    {
        public FileBaseInfo()
        {

        }
        public FileBaseInfo(string name, string path, bool isDir)
        {
            this.Name = name;
            this.Path = path;
            this.IsDirectory = isDir;
        }
        public string Name { get; set; }
        public string Path { get; set; }
        public bool IsDirectory { get; set; }
        public string Url { get; set; }
    }
    public class FileResponse
    {
        public string[] RootPath { get; set; }
        public string[] Paths { get; set; }
        public List<FileBaseInfo> Files { get; set; }
    }
}
